using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data;
using ContactBL = Microsoft.AceEngineering.SPM.SPMBusinessLayer;
using Microsoft.AceEngineering.SPM.SPMBusinessLayer;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;

public partial class Admin_ContactTypes : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
            ValidateUser();
        else
        {
            UserCDE userData = (UserCDE)Session["user"];
            if (userData.Time.AddMinutes(Int32.Parse(ConfigurationManager.AppSettings["AbsoluteExpiration"].ToString())) <= DateTime.Now)
                ValidateUser();
        }

        bool isAdmin = UsersBL.GetUserAdmin(Session["loginname"].ToString());
        if (!isAdmin)
            Response.Redirect("~/Home.aspx");
    }

    private string GiveMeAlias(string domainAndAlias)
    {
        domainAndAlias = domainAndAlias.ToLower();
        char[] strSeperator = new char[] { '\\' };
        string[] aryAuthUser = new string[2];
        aryAuthUser = domainAndAlias.Split(strSeperator);
        return aryAuthUser[1];
    }

    private void ValidateUser()
    {
        string currAlias = GiveMeAlias(HttpContext.Current.User.Identity.Name);
        UserCDE user = UsersBL.ValidateUser(currAlias);
        if (user.Active)
        {
            Session["user"] = user;
            Session["loginname"] = user.UserAlias;
        }
        else
        {
            Response.Redirect(Request.ApplicationPath + "/Errors/NoAccess.htm");
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                string strContactType = txtContactType.Text;
                ContactBL.ContactTypesBL.InsertContactType(txtContactType.Text, Session["loginname"].ToString());
                txtContactType.Text = null;
                gridContactTypeEnabled.DataBind();
                gridContactTypeDisabled.DataBind();
            }
            catch (Exception ex)
            {
                string strErrorType = ex.Message.ToString().Substring(0, 8);

                Page pge = (Page)this.Page;
                Microsoft.ACEEngineering.Utility.CommonProcess.APMErrorHandling(ex, Request.IsLocal, pge, Session, Request);
                if (!Request.IsLocal && strErrorType != "APMError")
                {
                    Server.ClearError();
                    Server.Transfer(Request.ApplicationPath + "/Errors/GenericErrorPage.htm");
                }
            }
        }
        else
        {
            StringBuilder msg = new StringBuilder("Please correct the following:\\n");
            foreach (IValidator aValidator in this.Validators)
            {
                if (!aValidator.IsValid)
                {
                    msg.Append("- " + aValidator.ErrorMessage + "\\n");
                }
            }
            Page pge = (Page)this.Page;
            Microsoft.ACEEngineering.Utility.CommonProcess.CreateMessageAlert(ref pge, msg.ToString(), "errorkey");
        }
    }

    protected void gridContactTypeEnabled_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gridContactTypeDisabled.DataBind();
    }

    protected void gridContactTypeDisabled_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gridContactTypeEnabled.DataBind();
    }

    protected void gridContactTypeEnabled_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        if (e.Exception != null)
        {
            Page pge = (Page)this.Page;
            Microsoft.ACEEngineering.Utility.CommonProcess.CreateMessageAlert(ref pge, "Duplicate Contact Type was entered.", "errorkey");
            e.ExceptionHandled = true;
            e.KeepInEditMode = true;
        }
    }

    protected void gridContactTypeEnabled_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (System.Web.UI.Control BaseControl in e.Row.Cells[0].Controls)
            {
                if (BaseControl.GetType().BaseType == typeof(LinkButton))
                {
                    LinkButton linkBtn = (LinkButton)BaseControl;
                    if (linkBtn.CommandName == "Delete")
                        linkBtn.OnClientClick = "return confirm('Are you sure you want to disable this Contact Type?');";
                }
            }

            ContactTypeCDE contact = (ContactTypeCDE)e.Row.DataItem;
            if (contact.ContactTypeID == "99998b54-da83-4d1a-858c-43a377b86c9e")
            {
                e.Row.Cells[0].Enabled = false;
            }
        }
    }
}
